package com.menggudf.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.menggudf.model.ResultModel;
import com.menggudf.model.SystemAdminModel;
import com.menggudf.service.MedicineService;

@Controller
@RequestMapping("/medicine")
public class MedicineController {
    
    @Autowired
    private MedicineService medicineService;
    
    @RequestMapping("add")
    @ResponseBody
    public ResultModel add(
            @RequestParam("name") String name,
            @RequestParam("ingredient") String ingredient,
            @RequestParam("traits") String traits,
            @RequestParam("effectType") String effectType,
            @RequestParam("indications") String indications,
            @RequestParam("specification") String specification,
            @RequestParam("dosage") String dosage,
            @RequestParam("adverseReactions") String adverseReactions,
            @RequestParam("taboo") String taboo,
            @RequestParam("precautions") String precautions,
            @RequestParam("price") String price,
            HttpSession httpSession){
        
        SystemAdminModel model = (SystemAdminModel) httpSession.getAttribute("SystemAdminModel");
        if(model==null || model.getAdminCode()==null || model.getAdminName()==null){
            return ResultModel.fail("session 过期", null);
        }
        return medicineService.add(name,ingredient,traits,effectType,indications,specification,dosage,adverseReactions,taboo,precautions,price,model.getAdminCode());
        
    }
    
    @RequestMapping("update")
    @ResponseBody
    public ResultModel update(
            @RequestParam("name") String name,
            @RequestParam(value="ingredient",required=false) String ingredient,
            @RequestParam(value="traits",required=false) String traits,
            @RequestParam(value="effectType",required=false) String effectType,
            @RequestParam(value="indications",required=false) String indications,
            @RequestParam(value="specification",required=false) String specification,
            @RequestParam(value="dosage",required=false) String dosage,
            @RequestParam(value="adverseReactions",required=false) String adverseReactions,
            @RequestParam(value="taboo",required=false) String taboo,
            @RequestParam(value="precautions",required=false) String precautions,
            @RequestParam(value="price",required=false) String price,
            HttpSession httpSession){
        SystemAdminModel model = (SystemAdminModel) httpSession.getAttribute("SystemAdminModel");
        if(model==null || model.getAdminCode()==null || model.getAdminName()==null){
            return ResultModel.fail("session 过期", null);
        }
        return medicineService.update(name,ingredient,traits,effectType,indications,specification,dosage,adverseReactions,taboo,precautions,price,model.getAdminCode());
        
    }
    
    @RequestMapping("get")
    @ResponseBody
    public Map<String, Object> get(
            @RequestParam("limit") int pageSize,
            @RequestParam("offset") int offset,
            @RequestParam(value="name",required=false) String name){
                return medicineService.get(name,pageSize,offset);
        
    }
    
    @RequestMapping("getDetail")
    @ResponseBody
    public ResultModel getDetail(
            @RequestParam(value="name",required=true) String name ){
                return medicineService.getDetail(name);
        
    }
    
    @RequestMapping("delete")
    @ResponseBody
    public ResultModel delete(
            @RequestParam(value="name",required=true) String name ){
                return medicineService.delete(name);
        
    }
    
    /**
     * text导入
     * */
/*    @RequestMapping("/importText")
    @ResponseBody
    public ResultModel importText(
            @RequestParam MultipartFile file,
            HttpServletRequest request, HttpServletResponse response,HttpSession httpSession) throws IOException{
        return medicineService.importText(file);
         
    }*/
    
    @RequestMapping("/createMedicine")
    @ResponseBody
    public ResultModel createMedicine(
            @RequestParam("name") String name,
            @RequestParam("skinTest") Integer skinTest,
            @RequestParam("usage") String usage,
            @RequestParam("details") String details,
            @RequestParam("sideEffect") String sideEffect,
            @RequestParam("taboo") String taboo){
        return medicineService.createMedicine(name,details,skinTest,usage,sideEffect,taboo);
        
    }
    
    @RequestMapping("/deleteMedicine")
    @ResponseBody
    public ResultModel deleteMedicine(
            @RequestParam(value="id",required=true) Integer id ){
                return medicineService.deleteMedicine(id);
        
    }
    
    @RequestMapping("/getMedicineInfo")
    @ResponseBody
    public ResultModel getMedicineInfo(
            @RequestParam(value="id",required=true) Integer id ){
                return medicineService.getMedicineInfo(id);
        
    }
    
    @RequestMapping("/getMedicine")
    @ResponseBody
    public Map<String, Object> getMedicine(
            @RequestParam("limit") int pageSize,
            @RequestParam("offset") int offset,
            @RequestParam(value="name",required=false) String name){
                return medicineService.getMedicine(name,pageSize,offset);
        
    }
    
    @RequestMapping("/updateMedicine")
    @ResponseBody
    public ResultModel updateMedicine(
            @RequestParam("id") Integer id,
            @RequestParam(value="name",required=false) String name,
            @RequestParam(value="skinTest",required=false) Integer skinTest,
            @RequestParam(value="usage",required=false) String usage,
            @RequestParam(value="details",required=false) String details,
            @RequestParam(value="sideEffect",required=false) String sideEffect,
            @RequestParam(value="taboo",required=false) String taboo){
        return medicineService.updateMedicine(id,details,skinTest,usage,sideEffect,taboo,name);
        
    }
    
    /**
     * excel导入
     * @throws IOException 
     * */
    @RequestMapping("/importExcelMedicine")
    @ResponseBody
    public ResultModel importExcel(
            @RequestParam MultipartFile file,
            HttpServletRequest request, HttpServletResponse response,HttpSession httpSession) throws IOException{
        return medicineService.importExcel(file);
         
    }
    
    @RequestMapping("/getMedicineNamesList")
    @ResponseBody
    public Map<String, Object> getMedicineNamesList(
            @RequestParam("limit") int pageSize,
            @RequestParam("offset") int offset,
            @RequestParam(value="name",required=false) String name){
                return medicineService.getMedicine(name,pageSize,offset);
        
    }
    
    @RequestMapping("/getNewCreatedMedicineList")
    @ResponseBody
    public Map<String, Object> getNewCreatedMedicineList(
            @RequestParam("limit") int pageSize,
            @RequestParam("offset") int offset,
            @RequestParam(value="name",required=false) String name){
                return medicineService.getNewCreatedMedicineList(name,pageSize,offset);
        
    }
    
}
